var getlocation = false;
var _Longitude = '';
var _Latitude = '';
var search ;
var token = localStorage.getItem("token");	
function getLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition);
    }else{
        alert("不支持定位");
    }
}
function showPosition(position){
    console.log("Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude);
    _Latitude = position.coords.latitude;
	_Longitude = position.coords.longitude;
    localStorage.setItem("Latitude",position.coords.latitude);
    localStorage.setItem("Longitude",position.coords.longitude); 
    search = {
		'province':'',
		'city':'',
		'lng':_Longitude,
		'lat':_Latitude,
		'page':'1',
		'pagesize':'1'
	}
    console.log(JSON.stringify(search))
    getbyposition(_Latitude,_Longitude);
    
    issign();
}

//获取医院
function getbyposition(_Latitude,_Longitude){
	hui.ajax({
	    type : 'POST',
	    url : globleobj['domain']+'/comhospital/getbyposition',
	    data : search,
	    beforeSend : function(){hui.loading();},
	    complete   : function(){hui.closeLoading();},
	    success : function(msg){
	    	console.log('getbyposition:'+msg)
	    	var data = JSON.parse(msg)
	    	if(data.rc != 0){
	    		hui.toast(data.msg);
	    		return false; 
	    	}
	    	var headName = data.list[0].name; 
	    	hui(".mycoctor").html(headName);
	    	getgroups(data.list[0]._id);
	    },
	    error : function(e){
	        console.log(JSON.stringify(e));
	        hui.iconToast('读取消息失败', 'warn');
	    } 
	}); 
}
//获取医院所有医生组
function getgroups(groupIds){
	console.log(groupIds)
	hui.ajax({
	    type : 'POST',
	    url : globleobj['domain']+'/comhospital/getgroups',
	    data : {'group_ids':'5af566d19d2c28de846f22a3,5af566d19d2c28de846f22a7'},
	    beforeSend : function(){hui.loading();},
	    complete   : function(){hui.closeLoading();},
	    success : function(msg){
	    	console.log('getgroups:'+msg)
	    	var data = JSON.parse(msg)
	    	if(data.rc != 0){
	    		hui.toast(data.msg);
	    		return false; 
	    	}
	    	var options = ''; 
	    	var dataLength = data.list.length > 2 ? 2 :data.list.length;
	    	for(var i= 0;i< dataLength; i++){
	    		options += '<li  onclick="teamDetails('+JSON.stringify(data.list[i]).replace(/"/g, "&quot;")+')">'
								+'<a href="javascript:void(0)">'
									+'<div class="hui-media-list-img">'
										+'<img src="../../img/3.jpg" />'
									+'</div>'
									+'<div class="hui-media-content">'
										+'<h1>'+data.list[i].captain.name+'</h1>'
										+'<p>'+data.list[i].captain.specialty+'</p>'
									+'</div>'
								+'</a>'
							+'</li>'
	    	}
	    	$('#allsign .lookMore').before(options);
	    	
	    },
	    error : function(e){
	        console.log(JSON.stringify(e));
	        hui.iconToast('读取消息失败', 'warn');
	    } 
	}); 
}
//否已签约
function issign(){
	hui.ajax({
	    type : 'POST',
	    url : globleobj['domain']+'/residents/issign',
	    data : {'token':token},
	    beforeSend : function(){hui.loading();},
	    complete   : function(){hui.closeLoading();},
	    success : function(msg){
	    	console.log("team:"+msg) 
	    	var data = JSON.parse(msg);
	    	if(data.rc == 1){ 
    		var options = '<li class="teamDetails" onclick="teamDetails('+JSON.stringify(data.group_info).replace(/"/g, "&quot;")+','+true+')">'
								+'<a href="javascript:void(0)">'
									+'<div class="hui-media-list-img">'
										+'<img src="../../img/3.jpg" />'
									+'</div>'
									+'<div class="hui-media-content">'
										+'<h1>'+data.group_info.name+'</h1>'
										+'<p>'+data.group_info.hos_name+'</p>'
									+'</div>'
								+'</a>'
							+'</li>'
	    	}
	    	hui('#issign').html(options);
	    	
	    	
	    },
	    error : function(e){
	        console.log(JSON.stringify(e));
	        hui.iconToast('读取消息失败', 'warn');
	    } 
	}); 
}


function teamDetails(captain,num){
	console.log(JSON.stringify(captain))
	console.log(num)
	var captain = JSON.stringify(captain);
	localStorage.setItem("captain",captain);
	localStorage.setItem("issignNum",num);
	hui.open('teamDetails.html',{},true);
}

hui.ready(function(){
	if(localStorage.getItem('Latitude')){
		
		 search = {
			'province':'',
			'city':'',
			'lng':localStorage.getItem('Longitude'),
			'lat':localStorage.getItem('Latitude'),
			'page':'1',
			'pagesize':'1'
		}
		getbyposition(localStorage.getItem('Latitude'),localStorage.getItem('Longitude'));
		issign();
	}else{		
		
		getLocation();
	}
	hui('#hui-back').click(function(){
	    hui.open('../homepage.html',{},true);
	});
		
	//跳转社区筛选
	hui('#goCommunitycity').click(function(){
	    hui.open('communitycity.html',{},true);
	});
		
	//跳转社区筛选
	hui('#look').click(function(){
	    hui.open('moreTearm.html',{},true);
	});
	
	//跳转团队详情
//	hui('.teamDetails').click(function(){
//	    hui.open('teamDetails.html');
//	});
	//选项卡
	hui.tab('.hui-tab');
	
    hui.plusReady(function(){
      	//console.log('plusReady');
    	
    });
});